use std::sync::{mpsc, Mutex};
use std::thread;

use once_cell::sync::Lazy;

use super::logger_thread;
use super::Message;

pub struct LoggerGlobal {
    pub thread_join_handle: Mutex<Option<thread::JoinHandle<()>>>,
    pub message_sender: Mutex<mpsc::Sender<Message>>,
}

impl LoggerGlobal {
    pub fn new() -> Self {
        let (thread_join_handle, message_sender) = logger_thread::init().unwrap();
        Self {
            thread_join_handle: Mutex::new(Some(thread_join_handle)),
            message_sender: Mutex::new(message_sender),
        }
    }
}

impl Default for LoggerGlobal {
    fn default() -> Self {
        Self::new()
    }
}

pub static LOGGER_GLOBAL: Lazy<LoggerGlobal> = Lazy::new(LoggerGlobal::new);
